﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TienTeTuong
{
    public partial class LunarCalendar : System.Web.UI.Page
    {
        public int Month = DateTime.Now.Month;
        public int Year = DateTime.Now.Year;
        public int[,] SolarDays = new int[6,7];
        public string[,] LunarDays = new string[6,7];
        public int[,] Events = new int[6,7];

        protected void Page_Load(object sender, EventArgs e)
        {
            Month = Request["m"] != null ? int.Parse(Request["m"]) : Month;
            Year = Request["y"] != null ? int.Parse(Request["y"]) : Year;

            ConvertDate cv = new ConvertDate();
            ArticleBUS bus = new ArticleBUS();
            //string ch = cv.convertSolar2Lunar(23, 1, 2012);
            //Response.Write(ch);

            DateTime time = new DateTime(Year, Month, 1);
            int DoW = (int)time.DayOfWeek;
            int DiM = DateTime.DaysInMonth(Year, Month);

            List<ArticleDTO> lstArt = bus.GetArticleDetailByTime(Month.ToString(), Year.ToString()) ;
            
            //Response.Write((int)DayOfWeek.Monday);
            int i = 0;
            int j = 0;
            int k = 0;


            while (j < DoW)
                j++;
            for (k = 1; k < DiM + 1; k++)
            {
                if (j > 6)
                {
                    j = 0;
                    i++;
                }
                SolarDays[i, j] = k;
                j++;
            }
            

            for (i = 0; i < 6; i++)

            {
                for (j = 0; j < 7; j++)
                {
                    if (SolarDays[i,j] != 0)
                    {
                        string lunarDay = cv.convertSolar2Lunar(SolarDays[i, j], Month, Year);
                        string lunarYear = cv.GetLunarYearName(int.Parse(lunarDay.Split('/')[2]));
                        LunarDays[i, j] = lunarDay.Split('/')[0] + "/" + lunarDay.Split('/')[1] + " " + lunarYear;
                        foreach (ArticleDTO dto in lstArt)
                        {
                            if (dto.CreatedDate != null && dto.CreatedDate.Value.Day == SolarDays[i, j])
                            {
                                LunarDays[i, j] += @"<br/><a href='ArticleDetail.aspx?id=" + dto.ArtID + "'>*" +
                                                   dto.Title + "</a>";
                                Events[i, j] = 1;
                            }
                        }
                        if (SolarDays[i,j] == DateTime.Now.Day && Month == DateTime.Now.Month && Year == DateTime.Now.Year)
                        {
                            Events[i, j] = 2;
                        }
                    }
                }
            }
        }
    }
}